<?php

/**
 *      客户管理控制器
 */

namespace Home\Controller;
use Think\Controller;

class FxcustController extends CommonController{

   public function _initialize() {
        parent::_initialize();
        $this->dbname = 'cash_user_info';
    }

   function _searchCount(&$map) {

	      $type = isset( $_REQUEST['selectType'] ) ? $_REQUEST['selectType'] : 'm';
        $time = isset( $_REQUEST['time'] ) && !empty(  $_REQUEST['time'] ) ? strtotime($_REQUEST['time']) : time();
        switch ( $type ) {
          case 'm':
              $title = date('Y',$time).'年';
              $word  = '每月用户';
              $type  = 'm月份';
              $where = ' creat_time BETWEEN '.strtotime(date('Y-01-01',$time)).' and '.strtotime(date('Y-12-31',$time)); break;
          case 'd': 
              $title = date('Y-m',$time).'月';
              $word  = '每日用户';
              $type  = 'd号';
              $where = ' creat_time BETWEEN '.strtotime(date('Y-m-01',$time)).' and '.strtotime(date('Y-m-t 23:59:59',$time));    break;
          case 'Y':
              $title = date('Y',$time).'年';
              $word  = '用户';
              $type  = 'Y年';
              $where = ' creat_time BETWEEN '.strtotime('2017-01-01').' and '.strtotime(date('Y'));                      break;
          case 'H':
              $title = date('Y-m-d',$time).'日';
              $word  = '每时用户';
              $type  = 'H时';
              $where = ' creat_time BETWEEN '.strtotime(date('Y-m-d 0:0:0',$time)).' and '.strtotime(date('Y-m-d 23:59:59',$time));break;     
        }
        $data['type'] = $type;
        $data['where']= $where;
        $data['title']= $title;
        $data['time'] = $time;
        $data['word'] = $word;
        return $data;

	}
	 
   public function _befor_index(){
         
        $data  = $this->_searchCount();
        $user  = M()->query( "SELECT count(*) as num, FROM_UNIXTIME(creat_time,'%".$data['type']."') as time from cash_user_info where ".$data['where']." group by time" );

        $dataset = ['time'=>array_column($user,'time'),'num'=>array_column($user,'num')];

        $this->assign('data',$data);
        $this->assign('dataset',json_encode($dataset));

   }
  
  
   public function xuqiu(){
        
        // M($this->dbname)->join('cash user_order as odr ON')  

  }
 
   public function laiyuan(){
    
        $user  = M()->query( "SELECT COUNT( CASE WHEN pid = 0 THEN 1 ELSE NULL END ) AS `被邀请`,COUNT( CASE WHEN pid > 0 THEN 1 ELSE NULL END ) AS `自主注册`FROM cash_user_info where ".I('where') );
        import("Org.Util.Chart");
        $legend = array_keys($user[0]);
        $data   = array_values($user[0]);
        $num    = array_sum($data);
        $title  = I('title').'用户来源分析 总量：'.$num;
        $chart  = new \Chart;
        $size   = 140; 
        $width  = 550; 
        $height = 300; 
        ob_end_clean();
        $chart->create3dpie($title,$data,$size,$height,$width,$legend);
   }
   
   public function jinzhan(){

        $list[] = M()->query("SELECT count(*) as '无借款申请' from cash_user_info  where id  not in (SELECT uid from cash_order) and ".I('where'));
        $list[] = M()->query("SELECT count(*) as '被拒绝'from cash_user_info  where id in  (SELECT uid from cash_order where STATUS BETWEEN -3 and 3 and uid not in (SELECT uid from cash_order where STATUS >= 4)) and ".I('where'));
        $list[] = M()->query("SELECT count(*) as '成功借款' from cash_user_info  where id in(SELECT uid from cash_order where STATUS >= 4 ) and ".I('where'));
        import("Org.Util.Chart");
        foreach($list as $key =>$val ){

            foreach( $val[0] as $k => $v ){

               $legend[] = $k;
               $data[]   = $v;
            }
          
        }
        $num    = array_sum($data);
        $title  = I('title').'用户借款分析 总量：'.$num;
        $chart  = new \Chart;
        $size   = 140; 
        $width  = 550; 
        $height = 300; 
        ob_end_clean();
        $chart->create3dpie($title,$data,$size,$height,$width,$legend);
   }
   
    public function yewu(){
    $this->_fenxi('uname','业务员分析',2);
   }
   
   public function qushi(){

  	  import("Org.Util.Chart");
      $chart = new \Chart;
      $type = $_REQUEST['type'];
  	  $title= $_REQUEST['title'].$_REQUEST['word']."增长趋势";
      $set  = json_decode($_REQUEST['dataset'],true);
      $size  =  0; 
      $width = 1500; 
      $height = 600;
      $data   = $set['num']; 
      $legend = $set['time'];
      ob_end_clean();
      $chart->createmonthline($title,$data,$size,$height,$width,$legend);
	}
	
	public function orderUP(){
	  import("Org.Util.Chart");
      $chart = new \Chart;
     // $type = $_REQUEST['type'];
  	  $title= $_REQUEST['title'];
      $set  = json_decode($_REQUEST['dataset'],true);
      $size  =  0; 
      $width = 1500; 
      $height = 600;
      $data   = $set['num']; 
      $legend = $set['time'];
      ob_end_clean();
      $chart->createmonthline($title,$data,$size,$height,$width,$legend);
	}


	

}